package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.platform.SplitAccountMapper;
import com.caiheng.api.dao.platform.SplitAccountRecordMapper;
import com.caiheng.api.dao.wxuser.UserIntegralRecordMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.dto.ChooseFeeDto;
import com.caiheng.api.entity.estate.*;
import com.caiheng.api.entity.platform.*;
import com.caiheng.api.entity.sys.SysConfig;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.entity.wxuser.UserIntegralRecord;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.pay.wxpay.WxAppPay;
import com.caiheng.api.rabbitmq.RabbitConfig;
import com.caiheng.api.rabbitmq.RabbitMqSender;
import com.caiheng.api.service.estate.PropertyFeeOrderService;
import com.caiheng.api.service.estate.PropertyFeeOrderTemplateService;
import com.caiheng.api.service.estate.PropertyService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.platfom.*;
import com.caiheng.api.service.sys.SysConfigService;
import com.caiheng.api.service.wxuser.UserService;
import com.caiheng.api.util.DateUtil;
import com.caiheng.api.util.MathUtil;
import com.caiheng.api.util.OkHttpClientUtil;
import com.vdurmont.emoji.EmojiParser;
import com.caiheng.api.dao.estate.*;
import com.caiheng.api.util.OrderUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 物业费订单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@Service
public class PropertyFeeOrderServiceImpl extends BaseServiceImpl<PropertyFeeOrderMapper, PropertyFeeOrder> implements PropertyFeeOrderService {

    @Resource
    private PropertyFeeOrderMapper feeOrderMapper;
    @Resource
    private PropertyFeeOrderTemplateMapper feeOrderTemplateMapper;
    @Resource
    private PropertyFeeOrderTemplateService feeOrderTemplateService;
    @Resource
    private UserIntegralRecordMapper userIntegralRecordMapper;
    @Resource
    private EstateRoomBangUserMapper roomBangUserMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private PropertyService propertyService;
    @Resource
    private EstateMapper estateMapper;
    @Resource
    private SysConfigService sysConfigService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private PlatformPayService platformPayService;
    @Resource
    private EstateRoomMapper estateRoomMapper;
    @Resource
    private EstateUserMapper estateUserMapper;
    @Resource
    private WxTemplateService wxTemplateService;
    @Resource
    private SplitAccountService splitAccountService;
    @Resource
    private SplitAccountMapper splitAccountMapper;
    @Resource
    private SplitAccountRecordMapper splitAccountRecordMapper;
    @Resource
    private PropertyFeeOrderInfoMapper feeOrderInfoMapper;
    @Resource
    private UserService userService;
    @Resource
    private WxPayTransactionService payTransactionService;

    @Resource
    private RabbitConfig rabbitConfig;
    private RabbitMqSender rabbitMqSender;
    @Value("${web.imgPrefix}")
    private String imgPrefix;//图片地址
    @Value("${web.appid}")
    private String appid;
    @Value("${web.secret}")
    private String secret;


    @Override
    @Transactional
    public long addOrder(PropertyFeeOrder order, User user) {
        try{
            int ret = feeOrderMapper.insert(order);
            if(ret <= 0){
                throw new TransactionalException("productOrderMapper insert error");
            }

            long count = getUnOpCount(order.getEstateId());
            String param = "{\"type\" : \"property\",\"count\":"+count+"}";
            RabbitTemplate rabbitTemplate = rabbitConfig.rabbitTemplate();
            rabbitMqSender = new RabbitMqSender(rabbitTemplate);
            rabbitMqSender.sendRabbitmqDirect("que_order_key",param);

            wxTemplateService.sendWxMsgAddPropertyOrder(user,order);
            return order.getId();
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("productOrderMapper insert error");
        }
    }

    @Override
    @Transactional
    public long addOrderByTemplate(User user, EstateRoom estateRoom, String checkIds, String beginMonth, String endMonth) {
        int ret = 0;
        PropertyFeeOrder feeOrder = new PropertyFeeOrder();
        Parameter parameter = parameterService.getParameterById(1);
        try{
            beginMonth = beginMonth.replace(" ","");
            endMonth = endMonth.replace(" ","");
            int roomId = estateRoom.getId();
            feeOrder.setUserId(user.getId());
            feeOrder.setRoomId(estateRoom.getId());
            feeOrder.setBeginTime(DateUtil.getDateByStr(beginMonth + "-01 00:00:00"));
            feeOrder.setEndTime(DateUtil.getDateByStr(endMonth + "-28 23:59:59"));
            feeOrder.setPlatform("wxmini");
            feeOrder.setEstateId(estateRoom.getEstateId());
            feeOrder.setEstateName(estateRoom.getEstateName());
            PlatformPay platformPay = null;
            Estate estate = estateMapper.selectById(estateRoom.getEstateId());
            if(estate!=null){
                Property property = propertyService.findById(estate.getPropertyId());
                if(property!=null){
                    feeOrder.setPropertyId(property.getId());
                    feeOrder.setPropertyName(property.getPropertyName());
                }
                feeOrder.setWyfYhRate(estate.getWyfYhRate());

                feeOrder.setPayPlatform(parameter.getWyfPayPlatform());

                if(parameter.getWyfPayPlatform() == 1 && estate.getPayPlatformId() != null && estate.getPayPlatformId() > 0){
                    platformPay = platformPayService.findById(estate.getPayPlatformId());
                    if(platformPay != null){
                        //先查询 小区绑定的支付商户是否存在
                        feeOrder.setPayPlatformId(platformPay.getId());
                    } else {
                        //小区绑定的不存在  再查询 物业绑定的支付商户是否存在
                        platformPay = platformPayService.getAccountByPropertyId(estate.getPropertyId());
                        if(platformPay != null){
                            feeOrder.setPayPlatformId(platformPay.getId());
                        }
                    }
                }

            }

            feeOrder.setState(0);
            feeOrder.setRefundPrice(new BigDecimal(0));
            feeOrder.setRefundStatus(0);
            feeOrder.setPhoneNum(estateRoom.getPhoneNum());
            feeOrder.setRoomNo(estateRoom.getRoomNo());
            feeOrder.setRoomAreas(estateRoom.getRoomArea());
            feeOrder.setNickName(user.getNickName());
            feeOrder.setRealName(estateRoom.getRealName());
            feeOrder.setMonths(0);
            feeOrder.setDkRate(0);
            feeOrder.setIsOpenReceipt(0);
            feeOrder.setPayMoney(new BigDecimal(0));
            String orderNo = estateRoom.getEstateId() + DateUtil.getSdfffTimes();
            if(platformPay != null){
                orderNo = estateRoom.getEstateId() + DateUtil.getSdfffTimes();
                if(parameter.getWyfPayPlatform() == 1){
                    orderNo = platformPay.getSysCode() + estateRoom.getEstateId() + DateUtil.getSdfffTimes();
                }
            }
            feeOrder.setOrderNo(orderNo);
            feeOrder.setOutTradeNo(orderNo);
            feeOrder.setCreateTime(new Date());
            feeOrder.setRoomType(estateRoom.getRoomType());
            feeOrder.setIsDel(0);

            feeOrder.setFreeMoney(new BigDecimal(0));
            feeOrder.setWyfYhPrice(new BigDecimal(0));
            feeOrder.setIsVerify(0);
            feeOrder.setVerifyCode(OrderUtil.getVerfiyNo());
            ret = feeOrderMapper.insert(feeOrder);
            if(ret <= 0){
                throw new TransactionalException("feeOrderMapper insert error");
            }

            int wyfMonthCount = 0;
            double totalPrice = 0d;
            String[] chIds = checkIds.split(",");
            for (String id : chIds){
                //查询模板数据
                PropertyFeeOrderTemplate template = feeOrderTemplateMapper.selectById(id);
                if(template != null){
                    template.setFeeOrderId(feeOrder.getId());
                    //计算 收费金额
                    if(template.getState() == 0){
                        totalPrice += template.getTotalPrice().doubleValue();
                        template.setFeeOrderId(feeOrder.getId());
                    }

                    if(template.getFeeType().equals("wyf")){//物业费 要包含上 垃圾处理费
                        //物业费
                        feeOrder.setWyfFee(template.getTotalPrice());
                        feeOrder.setWyfSinglePrice(template.getSinglePrice());
                        wyfMonthCount++;
                        //垃圾处理费
                        PropertyFeeOrderTemplate rubbish = feeOrderTemplateService.getTemplate(roomId,"rubbish",0,template.getYearNum(),template.getMonthNum());
                        if(rubbish != null){
                            if(rubbish.getState() == 0){
                                totalPrice += rubbish.getTotalPrice().doubleValue();
                                //垃圾处理费
                                feeOrder.setTrashFee(rubbish.getTotalPrice());
                                rubbish.setFeeOrderId(feeOrder.getId());
                                ret = feeOrderTemplateMapper.updateById(rubbish);
                                if(ret <= 0){
                                    throw new TransactionalException("feeOrderTemplateMapper updateById error");
                                }
                            }
                        }
                    } else if(template.getFeeType().equals("water")){//水费 要包含上 加压费
                        //水费
                        feeOrder.setWaterFee(template.getTotalPrice());
                        feeOrder.setWaterSinglePrice(template.getSinglePrice());
                        //加压费
                        PropertyFeeOrderTemplate jiaya = feeOrderTemplateService.getTemplate(roomId,"jiaya",0,template.getYearNum(),template.getMonthNum());
                        if(jiaya != null){
                            if(jiaya.getState() == 0){
                                totalPrice += jiaya.getTotalPrice().doubleValue();
                                //垃圾处理费
                                feeOrder.setJiaYaFee(jiaya.getTotalPrice());
                                jiaya.setFeeOrderId(feeOrder.getId());
                                ret = feeOrderTemplateMapper.updateById(jiaya);
                                if(ret <= 0){
                                    throw new TransactionalException("feeOrderTemplateMapper updateById error");
                                }
                            }
                        }
                    } else if(template.getFeeType().equals("car")){// 车位费
                        //车位费
                        feeOrder.setCarFee(template.getTotalPrice());
                        feeOrder.setCarSinglePrice(template.getSinglePrice());
                    } else if(template.getFeeType().equals("eletrict")){  // 电费
                        //电费
                        feeOrder.setElectricFee(template.getTotalPrice());
                        feeOrder.setElectricSinglePrice(template.getSinglePrice());
                    }
                }
            }
            feeOrder.setState(1);
            feeOrder.setTotalMoney(new BigDecimal(totalPrice));
            //物业费选择超过12个月以上，享受物业费折扣

            if(estate != null){
                if(wyfMonthCount >= 12) {// 全年折扣
                    if(estate.getWyfYhRate() !=null && estate.getWyfYhRate().doubleValue() > 9){
                        //物业费优惠折扣
                        //9.7折   10 - 9.7 = 0.3 * 10 = 3%；
                        double yhZheKou = estate.getWyfYhRate().doubleValue();
                        //0.03
                        double yhRate = (10.00 - yhZheKou) * 10 / 100;
                        //优惠的价格
                        double wyfYhPrice = MathUtil.getDoubleUp(feeOrder.getWyfFee().doubleValue() * yhRate);
                        feeOrder.setWyfYhRate(estate.getWyfYhRate());
                        feeOrder.setWyfYhPrice(new BigDecimal(wyfYhPrice));
                        totalPrice -= wyfYhPrice;
                    }

                } else if(wyfMonthCount >= 6 && wyfMonthCount < 12){// 半年折扣
                    //物业费优惠折扣
                    if(estate.getWyfYhRateHalf()!=null && estate.getWyfYhRateHalf().doubleValue() > 9){
                        //9.7折   10 - 9.7 = 0.3 * 10 = 3%；
                        double yhZheKou = estate.getWyfYhRateHalf().doubleValue();
                        //0.03
                        double yhRate = (10.00 - yhZheKou) * 10 / 100;
                        //优惠的价格
                        double wyfYhPrice = MathUtil.getDoubleUp(feeOrder.getWyfFee().doubleValue() * yhRate);
                        feeOrder.setWyfYhRate(estate.getWyfYhRateHalf());
                        feeOrder.setWyfYhPrice(new BigDecimal(wyfYhPrice));
                        totalPrice -= wyfYhPrice;
                    }
                }
            }


            feeOrder.setPayMoney(new BigDecimal(totalPrice));
            ret = feeOrderMapper.updateById(feeOrder);
            if(ret <= 0){
                throw new TransactionalException("feeOrderMapper insert error");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("feeOrderMapper insert error");
        }
        return feeOrder.getId();
    }


    @Override
    @Transactional
    public long addOrderByTemplate1(User user, EstateRoom estateRoom, String chooseFee, String beginMonth, String endMonth) {
        int ret = 0;
        PropertyFeeOrder feeOrder = new PropertyFeeOrder();
        Parameter parameter = parameterService.getParameterById(1);
        try{
            beginMonth = beginMonth.replace(" ","");
            endMonth = endMonth.replace(" ","");
            //取消之前未支付的订单
            QueryWrapper<PropertyFeeOrder> qw = new QueryWrapper<>();
            qw.eq("user_id",user.getId());
            qw.le("state",3);
            List<PropertyFeeOrder> list = feeOrderMapper.selectList(qw);
            if(CollUtil.isNotEmpty(list)){
                for (PropertyFeeOrder order : list){
                    order.setState(5);
                    ret = feeOrderMapper.updateById(order);
                    if(ret <= 0){
                        throw new TransactionalException("feeOrderMapper updateById error");
                    }
                }
            }

            int roomId = estateRoom.getId();
            feeOrder.setUserId(user.getId());
            feeOrder.setRoomId(estateRoom.getId());
            feeOrder.setBeginTime(DateUtil.getDateByStr(beginMonth + "-01 00:00:00"));
            feeOrder.setEndTime(DateUtil.getDateByStr(endMonth + "-28 23:59:59"));
            feeOrder.setPlatform("wxmini");
            feeOrder.setEstateId(estateRoom.getEstateId());
            feeOrder.setEstateName(estateRoom.getEstateName());

            PlatformPay platformPay = null;
            Estate estate = estateMapper.selectById(estateRoom.getEstateId());
            if(estate != null) {
                Property property = propertyService.findById(estate.getPropertyId());
                if(property !=null){
                    feeOrder.setPropertyId(property.getId());
                    feeOrder.setPropertyName(property.getPropertyName());
                }
                feeOrder.setWyfYhRate(estate.getWyfYhRate());
                feeOrder.setPayPlatform(1);
                if(parameter.getWyfPayPlatform() == 1 && estate.getPayPlatformId()!=null && estate.getPayPlatformId() > 0){
                    platformPay = platformPayService.findById(estate.getPayPlatformId());
                    if(platformPay != null){
                        //先查询 小区绑定的支付商户是否存在
                        feeOrder.setPayPlatformId(platformPay.getId());
                    } else {
                        //小区绑定的不存在  再查询 物业绑定的支付商户是否存在
                        platformPay = platformPayService.getAccountByPropertyId(estate.getPropertyId());
                        if(platformPay != null){
                            feeOrder.setPayPlatformId(platformPay.getId());
                        }
                    }
                }
            }


            feeOrder.setState(0);
            feeOrder.setRefundPrice(new BigDecimal(0));
            feeOrder.setRefundStatus(0);
            feeOrder.setPhoneNum(estateRoom.getPhoneNum());
            feeOrder.setRoomNo(estateRoom.getRoomNo());
            feeOrder.setRoomAreas(estateRoom.getRoomArea());
            feeOrder.setNickName(user.getNickName());
            feeOrder.setRealName(estateRoom.getRealName());
            feeOrder.setMonths(0);
            feeOrder.setDkRate(0);
            feeOrder.setIsOpenReceipt(0);
            feeOrder.setPayMoney(new BigDecimal(0));
            feeOrder.setIsDel(0);
            String orderNo = estateRoom.getEstateId() + DateUtil.getSdfffTimes();
            if(platformPay != null){
                orderNo = estateRoom.getEstateId() + DateUtil.getSdfffTimes();
                if(parameter.getWyfPayPlatform() == 1){
                    orderNo = platformPay.getSysCode() + estateRoom.getEstateId() + DateUtil.getSdfffTimes();
                }
            }
            feeOrder.setOrderNo(orderNo);
            feeOrder.setOutTradeNo(orderNo);
            feeOrder.setCreateTime(new Date());
            feeOrder.setRoomType(estateRoom.getRoomType());

            feeOrder.setFreeMoney(new BigDecimal(0));
            feeOrder.setWyfYhPrice(new BigDecimal(0));
            feeOrder.setIsVerify(0);
            feeOrder.setVerifyCode(OrderUtil.getVerfiyNo());
            ret = feeOrderMapper.insert(feeOrder);
            if(ret <= 0){
                throw new TransactionalException("feeOrderMapper insert error");
            }

            double totalPrice = 0d;
//            int wyfMonthCount = 0;
//            String[] chIds = chooseFee.split(",");

//            JSONArray jsonArray = JSONUtil.parseArray(chooseFee);
            List<ChooseFeeDto> chooseFeeDtos = JSONUtil.toList(chooseFee,ChooseFeeDto.class);

            double totalWyf = 0d, totalTrashFee = 0d, totalCarFee = 0d,
                    totalJiayaFee = 0d, totalWaterFee = 0d, totalEletrict = 0d;

            int wyfMonths = 0,wyfPrePayMonths = 0, trashMonths = 0, carMonths = 0, jiayaMonths = 0, waterMonths = 0,eletrictMonths = 0;


            double jiayaYouHui = 0d;
            double carYouHui = 0d;
            double rubbishYouHui = 0d;
            double wyfYouHui = 0d;

            double waterCount = 0d;
            double carCount = 0;
            double eletrictCount = 0d;

            Calendar calendar = Calendar.getInstance();
            int nowYear = calendar.get(Calendar.YEAR);
            int nowMonth = calendar.get(Calendar.MONTH) + 1; // Calendar的月份从0开始计数，所以需要加1

            for (ChooseFeeDto fee : chooseFeeDtos){
                PropertyFeeOrderInfo feeOrderInfo = new PropertyFeeOrderInfo();
                feeOrderInfo.setOrderId(feeOrder.getId());
                feeOrderInfo.setUserId(user.getId());
                feeOrderInfo.setEstateId(estate.getId());
                feeOrderInfo.setFeeType(fee.getFeeType());
                feeOrderInfo.setRoomId(estateRoom.getId());
                feeOrderInfo.setBeginDate(DateUtil.getDateByStr(fee.getBeginMonth() + "-01 00:00:00"));
                feeOrderInfo.setEndDate(DateUtil.getDateByStr(fee.getEndMonth() + "-28 23:59:59"));

                double attachPrice = 0d;
                double attachSinglePrice = 0d;
                double attachYouhuiPrice = 0d;


                double tempTotalPrice = 0d;
                List<Integer> ids = fee.getChooseList();
                int index = 0;
                for (Integer id : ids){

                    //是否是预交 0 - 不是预交  1 - 是预交费用
                    int isPrePay = 0;
                    //查询模板数据
                    PropertyFeeOrderTemplate template = feeOrderTemplateMapper.selectById(id);
                    if(template != null){
                        //判断是否是预交月份
                        //同年的情况   月份要大于当前月份的
                        if(Integer.parseInt(template.getYearNum()) == nowYear
                            && Integer.parseInt(template.getMonthNum()) > nowMonth){
                            isPrePay = 1;
                        }else if(Integer.parseInt(template.getYearNum()) > nowYear){//
                            //模板的年份要大于当前的年份
                            isPrePay = 1;
                        }

                        template.setIsPrePay(isPrePay);


                        feeOrderInfo.setSinglePrice(template.getSinglePrice());
                        //计算 收费金额
                        if(template.getState() == 0){
                            template.setFeeOrderId(feeOrder.getId());
                            if(template.getTotalPrice()!=null) {
                                tempTotalPrice += template.getTotalPrice().doubleValue();
                                // 优惠金额
                                if(template.getYouhuiPrice()!= null && template.getYouhuiPrice().doubleValue() > 0){
                                    tempTotalPrice -= template.getYouhuiPrice().doubleValue();
                                }
                            }
                        }
                        if(template.getFeeType().equals("water") || template.getFeeType().equals("eletrict")){
                            if(index == 0){
                                if(template.getBeginCount()!=null){
                                    feeOrderInfo.setBeginCount(template.getBeginCount().doubleValue());
                                }
                            }
                            if(index == ids.size() - 1){
                                if(template.getEndCount()!=null) {
                                    feeOrderInfo.setEndCount(template.getEndCount().doubleValue());
                                }
                            }
                        }

                        if(template.getFeeType().equals("wyf")){//物业费 要包含上 垃圾处理费
                            feeOrder.setWyfSinglePrice(template.getSinglePrice());
                            //物业费
                            if(template.getState() == 0){
                                totalWyf += template.getTotalPrice().doubleValue();
                                //如果是预交  +1
                                if(isPrePay == 1){
                                    wyfPrePayMonths ++;
                                }
                                wyfMonths ++;

                                //物业费的优惠金额
                                if(template.getYouhuiPrice() != null && template.getYouhuiPrice().doubleValue() > 0){
                                    wyfYouHui += template.getYouhuiPrice().doubleValue();
                                }
                            }

                            //垃圾处理费
                            PropertyFeeOrderTemplate rubbish = feeOrderTemplateService.getTemplate(roomId,"rubbish",0,template.getYearNum(),template.getMonthNum());
                            if(rubbish != null){
                                attachSinglePrice = rubbish.getSinglePrice().doubleValue();
                                if(rubbish.getState() == 0){
                                    rubbish.setFeeOrderId(feeOrder.getId());
                                    if(template.getTotalPrice()!=null) {

                                        tempTotalPrice += rubbish.getTotalPrice().doubleValue();
                                        //垃圾处理费
                                        totalTrashFee += rubbish.getTotalPrice().doubleValue();
                                        trashMonths ++;
                                        if(rubbish.getTotalPrice()!=null){
                                            attachPrice += rubbish.getTotalPrice().doubleValue();
                                        }
                                        if(rubbish.getYouhuiPrice()!=null){
                                            attachYouhuiPrice += rubbish.getYouhuiPrice().doubleValue();
                                        }

                                        // 垃圾处理费 优惠金额
                                        if(rubbish.getYouhuiPrice()!= null && rubbish.getYouhuiPrice().doubleValue() > 0){
                                            tempTotalPrice -= rubbish.getYouhuiPrice().doubleValue();
                                            rubbishYouHui += rubbish.getYouhuiPrice().doubleValue();
                                        }
                                    }
                                    ret = feeOrderTemplateMapper.updateById(rubbish);
                                    if(ret <= 0){
                                        throw new TransactionalException("feeOrderTemplateMapper updateById error");
                                    }
                                }
                            }

                            //垃圾处理费
                            PropertyFeeOrderTemplate rubbishGarage = feeOrderTemplateService.getTemplate(roomId,"rubbish",1,template.getYearNum(),template.getMonthNum());
                            if(rubbishGarage != null){
                                attachSinglePrice += rubbishGarage.getSinglePrice().doubleValue();
                                if(rubbishGarage.getState() == 0){
                                    rubbishGarage.setFeeOrderId(feeOrder.getId());
                                    if(template.getTotalPrice()!=null) {
                                        tempTotalPrice += rubbishGarage.getTotalPrice().doubleValue();
                                        //垃圾处理费
                                        totalTrashFee += rubbishGarage.getTotalPrice().doubleValue();
//                                        trashMonths ++;
                                        if(rubbishGarage.getTotalPrice()!=null){
                                            attachPrice += rubbishGarage.getTotalPrice().doubleValue();
                                        }
                                        if(rubbishGarage.getYouhuiPrice()!=null){
                                            attachYouhuiPrice += rubbishGarage.getYouhuiPrice().doubleValue();
                                        }

                                        // 垃圾处理费 优惠金额
                                        if(rubbishGarage.getYouhuiPrice()!= null && rubbishGarage.getYouhuiPrice().doubleValue() > 0){
                                            tempTotalPrice -= rubbishGarage.getYouhuiPrice().doubleValue();
                                            rubbishYouHui += rubbishGarage.getYouhuiPrice().doubleValue();
                                        }
                                    }
                                    ret = feeOrderTemplateMapper.updateById(rubbishGarage);
                                    if(ret <= 0){
                                        throw new TransactionalException("feeOrderTemplateMapper updateById error");
                                    }
                                }
                            }
                        } else if(template.getFeeType().equals("water")){//水费 要包含上 加压费
                            feeOrder.setWaterSinglePrice(template.getSinglePrice());
                            //水费
                            if(template.getState() == 0) {
                                if(template.getTotalPrice() !=null) {
                                    totalWaterFee += template.getTotalPrice().doubleValue();
                                    waterMonths ++;
                                }
                                if(template.getPaidCount()!=null){
                                    waterCount += template.getPaidCount().doubleValue();
                                }
                            }
                            //加压费
                            PropertyFeeOrderTemplate jiaya = feeOrderTemplateService.getTemplate(roomId,"jiaya",0,template.getYearNum(),template.getMonthNum());
                            if(jiaya != null){
                                if(jiaya.getState() == 0){
                                    jiaya.setFeeOrderId(feeOrder.getId());
                                    if(template.getTotalPrice()!=null) {
                                        tempTotalPrice += jiaya.getTotalPrice().doubleValue();
                                        // 加压费 优惠金额
                                        if(jiaya.getYouhuiPrice()!= null && jiaya.getYouhuiPrice().doubleValue() > 0){
                                            tempTotalPrice -= jiaya.getYouhuiPrice().doubleValue();
                                            jiayaYouHui += jiaya.getYouhuiPrice().doubleValue();
                                        }
                                        // 垃圾处理费
                                        totalJiayaFee += jiaya.getTotalPrice().doubleValue();
                                        attachPrice += jiaya.getTotalPrice().doubleValue();

                                        if(jiaya.getYouhuiPrice()!=null) {
                                            attachYouhuiPrice += jiaya.getYouhuiPrice().doubleValue();
                                        }
                                    }
                                    jiayaMonths ++;
                                    ret = feeOrderTemplateMapper.updateById(jiaya);
                                    if(ret <= 0){
                                        throw new TransactionalException("feeOrderTemplateMapper updateById error");
                                    }
                                }
                            }

                            //加压费
                            PropertyFeeOrderTemplate jiayaGarage = feeOrderTemplateService.getTemplate(roomId,"jiaya",1,template.getYearNum(),template.getMonthNum());
                            if(jiayaGarage != null){
                                if(jiayaGarage.getState() == 0){
                                    jiayaGarage.setFeeOrderId(feeOrder.getId());
                                    if(template.getTotalPrice()!=null) {
                                        tempTotalPrice += jiayaGarage.getTotalPrice().doubleValue();
                                        // 加压费 优惠金额
                                        if(jiayaGarage.getYouhuiPrice()!= null && jiayaGarage.getYouhuiPrice().doubleValue() > 0){
                                            tempTotalPrice -= jiayaGarage.getYouhuiPrice().doubleValue();
                                            jiayaYouHui += jiayaGarage.getYouhuiPrice().doubleValue();
                                        }
                                        // 垃圾处理费
                                        totalJiayaFee += jiayaGarage.getTotalPrice().doubleValue();
                                        attachPrice += jiayaGarage.getTotalPrice().doubleValue();

                                        if(jiayaGarage.getYouhuiPrice()!=null) {
                                            attachYouhuiPrice += jiayaGarage.getYouhuiPrice().doubleValue();
                                        }
                                    }
                                    ret = feeOrderTemplateMapper.updateById(jiayaGarage);
                                    if(ret <= 0){
                                        throw new TransactionalException("feeOrderTemplateMapper updateById error");
                                    }
                                }
                            }

                        } else if(template.getFeeType().equals("car")){// 车位费
                            //车位费
                            feeOrder.setCarSinglePrice(template.getSinglePrice());
                            if(template.getState() == 0) {
                                if (template.getTotalPrice() != null) {
                                    totalCarFee += template.getTotalPrice().doubleValue();
                                }
                                if (template.getPaidCount() != null) {
                                    carCount += template.getPaidCount().doubleValue();
                                }

                                if (template.getYouhuiPrice() != null && template.getYouhuiPrice().doubleValue() > 0) {
                                    tempTotalPrice -= template.getYouhuiPrice().doubleValue();
                                    carYouHui += template.getYouhuiPrice().doubleValue();
                                }
                                carMonths++;
                            }
                        } else if(template.getFeeType().equals("eletrict")){  // 电费
                            //电费
                            feeOrder.setElectricSinglePrice(template.getSinglePrice());
                            if(template.getState() == 0) {
                                if(template.getTotalPrice()!=null){
                                    totalEletrict += template.getTotalPrice().doubleValue();
                                }
                                if(template.getPaidCount()!=null) {
                                    eletrictCount += template.getPaidCount().doubleValue();
                                }

                                eletrictMonths ++;
                            }
                        }
                    }

                    ret = feeOrderTemplateMapper.updateById(template);
                    if(ret <= 0){
                        throw new TransactionalException("feeOrderTemplateMapper updateById error");
                    }
                    index++;
                }
                if(fee.getFeeType().equals("wyf")){
                    feeOrderInfo.setMonthCount(wyfMonths);
                }else if(fee.getFeeType().equals("water")){
                    feeOrderInfo.setMonthCount(waterMonths);
                    feeOrderInfo.setCount(waterCount);
                }else if(fee.getFeeType().equals("car")){
                    feeOrderInfo.setMonthCount(carMonths);
                    feeOrderInfo.setCount(carCount);
                }else if(fee.getFeeType().equals("eletrict")){
                    feeOrderInfo.setMonthCount(eletrictMonths);
                    feeOrderInfo.setCount(eletrictCount);
                }

                //当前费用类型下的 所有金额
                feeOrderInfo.setTotalPrice(new BigDecimal(tempTotalPrice));
                feeOrderInfo.setAttachPrice(new BigDecimal(attachPrice));
                feeOrderInfo.setAttachSinglePrice(new BigDecimal(attachSinglePrice));
                feeOrderInfo.setAttachYouhuiPrice(new BigDecimal(attachYouhuiPrice));

//                feeOrderInfo.setYouhuiPrice(new BigDecimal(wyfYhPrice));
                feeOrderInfo.setState(0);
                feeOrderInfo.setPayPrice(new BigDecimal(0));
                ret = feeOrderInfoMapper.insert(feeOrderInfo);
                if(ret <= 0){
                    throw new TransactionalException("feeOrderInfoMapper insert error");
                }
                //累加所有的金额
                totalPrice += tempTotalPrice;
            }

            //需要实缴金额
            feeOrder.setWyfFee(new BigDecimal(totalWyf));
            //应交金额
            feeOrder.setWyfTotalPrice(new BigDecimal(totalWyf));

            double wyfYhPrice = 0d;


            // 有初始现有优惠
            if(wyfYouHui > 0){
//                totalPrice = totalPrice - wyfYouHui;
                totalWyf =  totalWyf - wyfYouHui;
                feeOrder.setWyfYhPrice(new BigDecimal(wyfYouHui));
            } else if(wyfYouHui == 0){// 没有其他的优惠
                if(estate != null){
                    if(wyfPrePayMonths >= 12) {// 全年折扣
                        //物业费优惠折扣
                        //9.7折   10 - 9.7 = 0.3 * 10 = 3%；
                        if(estate.getWyfYhRate()!=null && estate.getWyfYhRate().doubleValue() > 9){
                            double yhZheKou = estate.getWyfYhRate().doubleValue();
                            //0.03
                            double yhRate = (10.00 - yhZheKou) * 10 / 100;
                            //优惠的价格
                            wyfYhPrice = MathUtil.getDoubleUp(feeOrder.getWyfFee().doubleValue() * yhRate);
                            feeOrder.setWyfYhRate(estate.getWyfYhRate());
                            feeOrder.setWyfYhPrice(new BigDecimal(wyfYhPrice));
                            totalPrice = totalPrice - wyfYhPrice;
                            totalWyf =  totalWyf - wyfYhPrice;
                        }
                    } else if(wyfPrePayMonths >= 6 && wyfPrePayMonths < 12){// 半年折扣
                        //物业费优惠折扣
                        //9.7折   10 - 9.7 = 0.3 * 10 = 3%；
                        if(estate.getWyfYhRate()!=null && estate.getWyfYhRateHalf().doubleValue() > 9){
                            double yhZheKou = estate.getWyfYhRateHalf().doubleValue();
                            //0.03
                            double yhRate = (10.00 - yhZheKou) * 10 / 100;
                            //优惠的价格
                            wyfYhPrice = MathUtil.getDoubleUp(feeOrder.getWyfFee().doubleValue() * yhRate);
                            feeOrder.setWyfYhRate(estate.getWyfYhRateHalf());
                            feeOrder.setWyfYhPrice(new BigDecimal(wyfYhPrice));

                            totalPrice = totalPrice - wyfYhPrice;
                            totalWyf =  totalWyf - wyfYhPrice;
                        }
                    }
                }
            }


            feeOrder.setWyfMonths(wyfMonths);
            feeOrder.setTrashMonths(trashMonths);
            feeOrder.setCarMonths(carMonths);
            feeOrder.setWaterMonths(waterMonths);
            feeOrder.setJiayaMonths(jiayaMonths);
            feeOrder.setElectricMonths(eletrictMonths);

            feeOrder.setWyfFee(new BigDecimal(totalWyf));
            feeOrder.setTrashFee(new BigDecimal(totalTrashFee));
            feeOrder.setCarFee(new BigDecimal(totalCarFee));
            feeOrder.setWaterFee(new BigDecimal(totalWaterFee));
            feeOrder.setJiaYaFee(new BigDecimal(totalJiayaFee));
            feeOrder.setElectricFee(new BigDecimal(totalEletrict));


            feeOrder.setCarYouhui(new BigDecimal(carYouHui));
            feeOrder.setTrashYouhui(new BigDecimal(rubbishYouHui));
            feeOrder.setJiaYaYouhui(new BigDecimal(jiayaYouHui));

            feeOrder.setState(1);
            feeOrder.setTotalMoney(new BigDecimal(totalPrice));
            //物业费选择超过12个月以上，享受物业费折扣
            feeOrder.setPayMoney(new BigDecimal(totalPrice));

            if(totalPrice <= 0){
                throw new TransactionalException("feeOrderMapper insert error");
            }
            ret = feeOrderMapper.updateById(feeOrder);
            if(ret <= 0){
                throw new TransactionalException("feeOrderMapper insert error");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("feeOrderMapper insert error");
        }
        return feeOrder.getId();
    }


    @Override
    public long getUnOpCount(int estateId) {
        QueryWrapper<PropertyFeeOrder> qw = new QueryWrapper<>();
        qw.select("id");
        qw.eq("estate_id",estateId);
        qw.eq("state",0);
        return feeOrderMapper.selectCount(qw);
    }

    @Override
    public List<PropertyFeeOrder> queryListByUserId(int userId, int state, int pageIndex, int pageSize) {
        try{
            QueryWrapper<PropertyFeeOrder> qw1 = new QueryWrapper<>();
            qw1.eq("user_id",userId);
            if(state != -1){
                qw1.eq("state",state);
            }
            qw1.orderByDesc("create_time");
            List<PropertyFeeOrder> list = feeOrderMapper.getPageEntityList(qw1,pageIndex,pageSize);
            if(CollUtil.isNotEmpty(list)){
                for (PropertyFeeOrder feeOrder : list){
                    if(feeOrder.getBeginTime()!=null){
                        feeOrder.setBeginDate(DateUtil.getTimesStr(feeOrder.getBeginTime(),DateUtil.sdfMonth));
                        feeOrder.setEndDate(DateUtil.getTimesStr(feeOrder.getEndTime(),DateUtil.sdfMonth));
                    }
                    if(StrUtil.isNotEmpty(feeOrder.getReceiptFilePath())){
                        feeOrder.setReceiptFilePath(imgPrefix + feeOrder.getReceiptFilePath());
                    }
                    if(StrUtil.isNotEmpty(feeOrder.getReceiptImgUrl())){
                        feeOrder.setReceiptImgUrl(imgPrefix + feeOrder.getReceiptImgUrl());
                    }
                }
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新订单的状态
     * @return
     */
    @Override
    public int updateOrderState(PropertyFeeOrder feeOrder, int state) {
        feeOrder.setState(state);
        return feeOrderMapper.updateById(feeOrder);
    }

    @Override
    @Transactional
    public int payOver(PropertyFeeOrder feeOrder, List<PropertyFeeOrderTemplate> orderTemplates) {
        int ret = 0;
        try{
            ret = feeOrderMapper.updateById(feeOrder);
            if(ret <= 0){
                throw new TransactionalException("productOrderMapper updateById error");
            }

            Estate estate = estateMapper.selectById(feeOrder.getEstateId());
            if(estate != null){
                //开启赠送积分功能
                if(estate.getIsOpenIntegral() != null && estate.getIsOpenIntegral() == 1){

                    Date beginDate = estate.getOpenBeginDate();
                    Date endDate = estate.getOpenEndDate();
                    if(beginDate.getTime() < new Date().getTime() && new Date().getTime() < endDate.getTime()){
                        String configValue = sysConfigService.selectConfigByKey("module.integral_count");
                        double totalMoney = feeOrder.getPayMoney().doubleValue();
                        long totalIntegral = (long) totalMoney * Integer.parseInt(configValue) * estate.getIntegralMultiple();
                        SplitAccount account = splitAccountService.getAccountByUserId(feeOrder.getUserId(), 4, feeOrder.getUserId());
                        if(account != null){
                            account.setIntegralCount(account.getIntegralCount() + totalIntegral);
                            ret = splitAccountMapper.updateById(account);
                            if(ret <= 0){
                                throw new TransactionalException("productOrderMapper updateById error");
                            }
                            SplitAccountRecord accountRecord = new SplitAccountRecord();
                            accountRecord.setUserId(feeOrder.getUserId());
                            accountRecord.setAccountId(account.getId());
                            accountRecord.setCreateTime(new Date());
                            accountRecord.setFromType(16);
                            accountRecord.setRemark("交物业费获取积分");
                            //1 - 进账  0 - 出账
                            accountRecord.setInOrOut(1);
                            accountRecord.setSplitMoney(new BigDecimal(totalIntegral));
                            ret = splitAccountRecordMapper.insert(accountRecord);
                            if (ret <= 0) {
                                throw new TransactionalException("splitAccountRecordMapper insert error");
                            }
                        }
                    }
                }
            }




            QueryWrapper<EstateRoomBangUser> qwBangUser = new QueryWrapper<>();
            qwBangUser.eq("estate_id",feeOrder.getEstateId());
            qwBangUser.eq("bang_wx_user_id",feeOrder.getUserId());
            qwBangUser.eq("room_id",feeOrder.getRoomId());
            qwBangUser.last("limit 1");
            EstateRoomBangUser bangUser = roomBangUserMapper.selectOne(qwBangUser);
            if(bangUser == null){
                bangUser = new EstateRoomBangUser();
                bangUser.setBangWxUserId(feeOrder.getUserId());
                bangUser.setEstateId(feeOrder.getEstateId());
                bangUser.setRoomId(feeOrder.getRoomId());
                ret = roomBangUserMapper.insert(bangUser);
                if(ret <= 0){
                    throw new TransactionalException("roomBangUserMapper insert error");
                }
            }

            QueryWrapper<PropertyFeeOrderInfo> qwOrderInfo = new QueryWrapper<>();
            qwOrderInfo.eq("order_id",feeOrder.getId());
            qwOrderInfo.eq("state", 0);
            List<PropertyFeeOrderInfo> orderInfos = feeOrderInfoMapper.selectList(qwOrderInfo);
            if(CollUtil.isNotEmpty(orderInfos)){
                for (PropertyFeeOrderInfo orderInfo : orderInfos){
                    orderInfo.setState(1);
                    feeOrderInfoMapper.updateById(orderInfo);

                    //超时支付的时候 订单id 已经重置成0，通过info 表找出对应的月份，进行处理
                    if(CollUtil.isEmpty(orderTemplates)){

                        Calendar calendarBegin = Calendar.getInstance();
                        calendarBegin.setTime(orderInfo.getBeginDate());

                        int beginYear = calendarBegin.get(Calendar.YEAR);
                        int beginMonth = calendarBegin.get(Calendar.MONTH) + 1;

                        Calendar calendarEnd = Calendar.getInstance();
                        calendarEnd.setTime(orderInfo.getEndDate());

                        int endYear = calendarEnd.get(Calendar.YEAR);
                        int endMonth = calendarEnd.get(Calendar.MONTH) + 1;

                        QueryWrapper<PropertyFeeOrderTemplate> qwTemp = new QueryWrapper<>();
                        qwTemp.eq("room_id", orderInfo.getRoomId());
                        qwTemp.eq("state", 0);
                        qwTemp.eq("fee_type", orderInfo.getFeeType());
                        qwTemp.between("year_num", beginYear, endYear);
                        qwTemp.between("month_num", beginMonth, endMonth);
                        List<PropertyFeeOrderTemplate> templates = feeOrderTemplateService.list(qwTemp);
                        if(CollUtil.isNotEmpty(templates)){
                            for (PropertyFeeOrderTemplate template : templates){
                                template.setFeeOrderId(feeOrder.getId());
                                template.setState(1);
                                feeOrderTemplateService.updateById(template);
                            }
                        }
                    }
                }
            }



        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("payOver updateById error");
        }
        return ret;
    }


    @Override
    @Transactional
    public int cancelOrder(int userId) {
        try{
            QueryWrapper<PropertyFeeOrder> qw = new QueryWrapper<>();
            qw.eq("user_id",userId);
            qw.le("state",3);
            List<PropertyFeeOrder> list = feeOrderMapper.selectList(qw);
            if(CollUtil.isNotEmpty(list)){
                for (PropertyFeeOrder feeOrder : list){
                    feeOrder.setState(5);
                    feeOrderMapper.updateById(feeOrder);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    @Transactional
    public int cancelNoPayOrder(PropertyFeeOrder feeOrder) {
        int ret = 0;
        try{
            feeOrder.setState(5);
            ret = feeOrderMapper.updateById(feeOrder);
            if(ret <= 0){
                throw new TransactionalException("feeOrderMapper updateById error");
            }

//            List<PropertyFeeOrderTemplate> templates = feeOrderTemplateService.selectListByColumn("fee_order_id",feeOrder.getId());
//            if(CollUtil.isNotEmpty(templates)){
//                for (PropertyFeeOrderTemplate template : templates){
//                    template.setFeeOrderId(0L);
//                    ret = feeOrderTemplateMapper.updateById(template);
//                    if(ret <= 0){
//                        throw new TransactionalException("feeOrderMapper updateById error");
//                    }
//                }
//            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("cancelNoPayOrder updateById error");
        }
        return ret;
    }

    @Override
    @Transactional
    public int queryOrder(PropertyFeeOrder feeOrder) {
        int ret = 0;
        try{
            PlatformPay platformPay = null;
            if(feeOrder != null){
                platformPay = platformPayService.findById(feeOrder.getPayPlatformId());
                if(platformPay!= null){
                    String orderNo = feeOrder.getOutTradeNo();
                    //String out_trade_no, PlatformPay platformPay
                    String response = platformPayService.orderQuery(orderNo,platformPay);
                    System.out.println("物业费订单查询: 订单号：" + orderNo + ", 返回结果：" + response);
                    //{"errCode":"NO_ORDER","errMsg":"无法找到指定的订单","responseTimestamp":"2023-08-24 09:12:03"}
                    //{"payTime":"2023-08-23 17:40:28","connectSys":"UNIONPAY","delegatedFlag":"N",
                    // "merName":"家和物业管理有限公司","mid":"8983711651302GH","settleDate":"2023-08-23","settleRefId":"36020015042N",
                    // "tid":"8699HPNY","totalAmount":20565,"couponAmount":0,"attachedData":"WYF","chnlCost":"758",
                    // "targetMid":"590686512","responseTimestamp":"2023-08-24 09:12:02","errCode":"SUCCESS","seqId":"36020015042N",
                    // "merOrderId":"35XY153620230823174025","refundAmount":0,"status":"WAIT_BUYER_PAY","targetSys":"WXPay"}
                    JSONObject jsonObject = JSONUtil.parseObj(response);
                    String errCode = jsonObject.getStr("errCode");
                    String status = jsonObject.getStr("status");
                    if(errCode.equals("NO_ORDER") || (errCode.equals("SUCCESS") && status.equals("WAIT_BUYER_PAY"))){
                        orderNo = feeOrder.getOrderNo();
                        System.out.println("物业费订单查询, 继续查询: 订单号：" + orderNo);
                        response = platformPayService.orderQuery(orderNo,platformPay);
                        System.out.println("物业费订单查询, 继续查询: " + response);
                        jsonObject = JSONUtil.parseObj(response);
//                            if(jsonObject!=null && jsonObject.containsKey("status")) {
//                                trade_no = jsonObject.getStr("merOrderId");
//                                status = jsonObject.getStr("status");
//                                errCode = jsonObject.getStr("errCode");
//                            }
                    }
                    if(jsonObject!=null && jsonObject.containsKey("status")){
                        String trade_no = jsonObject.getStr("merOrderId");
                        status = jsonObject.getStr("status");

                        if(status.equals("TRADE_SUCCESS")){
                            String payTime = jsonObject.getStr("payTime");
                            long totalAmount = jsonObject.getInt("totalAmount");
                            double total_amount_double = totalAmount / 100;
                            //订单状态   0 - 交费申请 1 - 已生成交费清单 2 - 待支付
                            // 3- 支付中  4 - 已支付 5 - 订单取消
                            feeOrder.setState(4);
                            feeOrder.setTransactionId(trade_no);
                            feeOrder.setPayMoney(new BigDecimal(total_amount_double));
                            feeOrder.setPayTime(DateUtil.fomatTime(payTime));

                            QueryWrapper<PropertyFeeOrderTemplate> qwOrder = new QueryWrapper<>();
                            qwOrder.eq("fee_order_id",feeOrder.getId());
                            qwOrder.eq("state", 0);
                            List<PropertyFeeOrderTemplate> orderTemplates = feeOrderTemplateService.list(qwOrder);
                            if(CollUtil.isNotEmpty(orderTemplates)){
                                for (PropertyFeeOrderTemplate template : orderTemplates){
                                    template.setState(1);
                                    feeOrderTemplateService.updateById(template);
                                }
                            }

                            ret = payOver(feeOrder,orderTemplates);
                            if(ret <= 0){
                                throw new TransactionalException("feeOrderMapper updateById error");
                            }
                            User userOrder = userService.findById(feeOrder.getUserId());
                            // 添加 微信支付的 动账记录
                            QueryWrapper<WxPayTransaction> qw = new QueryWrapper<>();
                            qw.eq("fee_type","wyf");
                            qw.eq("user_id",feeOrder.getUserId());
                            qw.eq("transaction_id",trade_no);
                            qw.last("limit 1");
                            WxPayTransaction transaction = new WxPayTransaction();
                            if(transaction == null){
                                transaction = new WxPayTransaction();
                                transaction.setTransactionNo(trade_no);
                                transaction.setEstateId(feeOrder.getEstateId());
                                transaction.setEstateName(feeOrder.getEstateName());
                                transaction.setPayType(1);// 0 -微信支付
                                transaction.setTransactionId(trade_no);
                                transaction.setUserId(feeOrder.getUserId());
                                transaction.setFeeType("wyf");
                                transaction.setTotalMoney(new BigDecimal(total_amount_double));
                                transaction.setOutTradeNo(trade_no);
                                if(userOrder!=null && StrUtil.isNotEmpty(userOrder.getNickName())){
                                    transaction.setNickName(EmojiParser.parseToAliases(userOrder.getNickName()));
                                }
                                transaction.setCreateTime(DateUtil.fomatTime(payTime));
                                transaction.setInOrOut(1);//动账  0 - 出账 1 - 进账
                                transaction.setRemark("物业费支付");
                                transaction.setPayPlatformId(feeOrder.getPayPlatformId());
                                boolean bl = payTransactionService.save(transaction);
                                if(!bl){
                                    throw new TransactionalException("payTransactionService updateById error");
                                }
                            }
                            //发往后台 生成收据图片https://www.caihengyun.com/admin/common/download/settle/1265
                            //https://www.caihengyun.com/admin/common/download/settle
                            OkHttpClientUtil.getInstance().getData("https://www.caihengyun.com/admin/common/download/settle/" + feeOrder.getId());
                        } else{

                            feeOrder.setState(5);
                            ret = feeOrderMapper.updateById(feeOrder);
                            if(ret <= 0){
                                throw new TransactionalException("feeOrderMapper updateById error");
                            }

//                            List<PropertyFeeOrderTemplate> templates = feeOrderTemplateService.selectListByColumn("fee_order_id",feeOrder.getId());
//                            if(CollUtil.isNotEmpty(templates)){
//                                for (PropertyFeeOrderTemplate template : templates){
//                                    template.setFeeOrderId(0L);
//                                    ret = feeOrderTemplateMapper.updateById(template);
//                                    if(ret <= 0){
//                                        throw new TransactionalException("feeOrderMapper updateById error");
//                                    }
//                                }
//                            }
                        }
                    }
                }
            }
            ret = 1;
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("query FeeOrder system error");
        }
        return ret;
    }

}
